package io.github.ali.commons.bean;

import io.github.ali.commons.beanutils.BeanCopierUtils;

/**
 * 深度拷贝转换
 * 
 * @author fuli
 * @date 2019年2月13日
 * @version 1.0.0
 */
public interface DeeplyConvertable {
	default <T>T convertTo(Class<T> targetClass){
		if (targetClass == null) {
			return null;
		}
		T instance = null;
		try {
			instance = targetClass.newInstance();
			BeanCopierUtils.deeplyCopyAttribute(this, instance);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return instance;
	}
	@SuppressWarnings("unchecked")
	default <T extends DeeplyConvertable>T convertFrom(Object source){
		if (source == null) {
			return null;
		}
		BeanCopierUtils.deeplyCopyAttribute(source, this);
		return (T)this;
	}
}
